<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleleaf.org">
<head th:insert="base">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form action="" method="post" class="layui-form layui-form-pane">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    资源列表
                </label>
                <input type="hidden" name="roleCode" id="roleCode"/>
                <table  class="layui-table layui-input-block">
                    <tbody id="authorize_table">
                    </tbody>
                </table>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">确认授权</button>
            </div>
        </form>
    </div>
</div>
<script th:inline="javascript">
    layui.use(['form','layer','jquery','permissionUtil','tool'], function(){
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var permissionUtil = layui.permissionUtil;
        var tool = layui.tool;

        var roleCode = [[${roleCode}]];
        var permissionStr = [[${permissionStr}]];

        init_form();

        //监听提交
        form.on('submit(add)', function(data){
            //异步提交
            var arr = new Array();
            $("input:checkbox[name='permissionCode']:checked").each(function(i){
                arr[i] = $(this).val();
            });
            data.field.permissionCode = arr.join(',');
            var res = tool.submit("/back/role/authorize",{roleCode:data.field.roleCode,permissionCodes:data.field.permissionCode});
            if(res.code == return_200){
                layer.msg(res.msg,{icon:msg.success},function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                })
            }else{
                layer.msg(res.msg,{icon:msg.error},function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                })
            }

            return false;
        });


        form.on('checkbox(father)', function(data){
            if(data.elem.checked){
                $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                form.render();
            }else{
                $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                form.render();
            }
        });

        function init_form(){
            $("#roleCode").val(roleCode);
            //初始化资源权限
            permissionUtil.createPremission("authorize_table",false,permissionStr);
            form.render();
        }
    });
</script>
</body>
</html>